package com.bestv.search.management.datasyn;

import com.bestv.search.common.dataSource.DataSourceKey;
import com.bestv.search.common.dataSource.DataSourceKeyUtil;
import com.bestv.search.common.model.Info;
import com.bestv.search.common.service.InfoManager;
import com.bestv.search.common.util.XmlUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.List;

public class NewXMLProgramProcess {

    static ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext-dao.xml",
            "applicationContext-resource.xml", "applicationContext-service.xml", "security.xml"});
    static InfoManager vm = (InfoManager) context.getBean("infoManager");

    /**
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        for (int k = 20100119; k <= 20100119; ++k) {
            System.out.println(k);
            Thread.sleep(2000);
            final String dirPrefix = String.valueOf(k);
            File dir = new File("E:\\61.8.167.165");
            if (!dir.exists())
                dir.mkdir();
            if (!dir.isDirectory()) {
                System.out.println("dir is wrong!");
                System.exit(-1);
            }
            File[] dirs = dir.listFiles(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    if (name.startsWith(dirPrefix))
                        return true;
                    return false;
                }
            });
            for (File f : dirs) {
                System.out.println("deal with " + f.getAbsolutePath() + "....");
                File[] files = f.listFiles(new FilenameFilter() {
                    public boolean accept(File dir, String name) {
                        if (name.startsWith("Program") && name.endsWith(".xml"))
                            return true;
                        return false;
                    }
                });
                int length = files.length;
                System.out.println(length + " files to deal...");
                if (length > 2500) {
                    int num = length / 2500; // length = num * 1500 + yushu
                    File[] tempFiles;
                    for (int i = 0; i < num; ++i) {
                        tempFiles = Arrays.copyOfRange(files, i * 2500, i * 2500 + 2500);
                        saveXmlDateToDB(tempFiles);
                    }
                    if (length > num * 2500) {
                        tempFiles = Arrays.copyOfRange(files, num * 2500, length);
                        saveXmlDateToDB(tempFiles);
                    }
                } else {
                    File[] tempFiles = Arrays.copyOfRange(files, 0, length);
                    saveXmlDateToDB(tempFiles);
                }
            }
        }

        System.out.println("total Time: " + (System.currentTimeMillis() - start));
    }

    public static void saveXmlDateToDB(File[] files) {
        System.out.print("...files: " + files.length);
        List<Info> list = XmlUtil.nodesToPrograms(XmlUtil.getAllNodesWithProgramAttribute(files, "Program"));
        long start = System.currentTimeMillis();
        DataSourceKey dsKey = DataSourceKeyUtil.convert("HDQ");
        vm.saveUpdateInfoList(dsKey, list);
        System.out.print("save finished! ");
        System.out.println(System.currentTimeMillis() - start + "ms");
    }

    public static File[] getFiles(String dir, final String preFix) {
        File file = new File(dir);
        if (!file.isDirectory())
            file.mkdir();
        File[] fileList = file.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                if (name.startsWith(preFix))// 15||name.startsWith("Program_55")||name.startsWith("Program_56")||name.startsWith("Program_57")||name.startsWith("Program_58")||name.startsWith("Program_59"))
                    return true;
                return false;
            }
        });
        return fileList;
    }
}
